Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2021.7: changes to entities in numeric_state above/below are ignored #52784

Closed
Troon opened this issue Jul 9, 2021 · 3 comments
Closed

2021.7: changes to entities in numeric_state above/below are ignored #52784

Troon opened this issue Jul 9, 2021 · 3 comments
Labels

Comments

@Troon
Copy link
Contributor

Troon commented Jul 9, 2021

The problem

When an entity is used in a numeric_state trigger's above or below declaration, changes to that entity do not result in the trigger firing when it seems logical that it should.

Example automation included below.

image

Moving the test_sensor across the test_threshold value correctly triggers the automations; but moving the test_threshold across the test_sensor value does nothing.

I would have expected moving the threshold to behave in the same way, otherwise I don't understand the point of the new feature.

What is version of Home Assistant Core has the issue?

2021.7

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

https://www.home-assistant.io/docs/automation/trigger/#numeric-state-trigger

Example YAML snippet

- alias: Test on
  id: b9fd928a-8749-428c-92d2-1540da5cb424

  trigger:
    - platform: numeric_state
      entity_id: input_number.test_sensor
      below: input_number.test_threshold

  action:
    - service: homeassistant.turn_on
      entity_id: input_boolean.test_trigger

- alias: Test off
  id: 34535ac7-e711-4882-8f6f-90948093c1ea

  trigger:
    - platform: numeric_state
      entity_id: input_number.test_sensor
      above: input_number.test_threshold

  action:
    - service: homeassistant.turn_off
      entity_id: input_boolean.test_trigger

Anything in the logs that might be useful for us?

No response

Additional information

New feature in 2021.7. From the release notes:

Referencing other entities in triggers and conditions
A small, but possibly helpful, change to our script and automations. You can now reference other entities for the above/below values of numeric state triggers and conditions. Both sensors and number entities can be used.

@tomlut
Copy link

tomlut commented Jul 9, 2021

This is also the case for numeric state conditions using input number helpers.

@frenck
Copy link
Member

frenck commented Jul 22, 2021

It kinda makes sense and kinda doesn't make sense. But I see the use case.

The thing is, the entity_id is the trigger and created the from->to states, while above/below are conditions.

trigger:
- platform: numeric_state
  entity_id: input_number.test_sensor
  below: input_number.test_threshold

In this case, input_number.test_sensor is monitored and tracked. above/below are basically conditions and only checked when the from & to states for a trigger fired by a change to the monitored entity ID fires.

It becomes even harder when talking about templates in this case. IMHO, we should document how this works.

More importantly, how to write automation that work with this :)

For example, I generally almost never put conditions in my triggers. I just trigger, and later figure out if it should run or not.

- alias: Test
  id: b9fd928a-8749-428c-92d2-1540da5cb424

  trigger:
    - platform: state
      entity_id: 
        - input_number.test_sensor
        - input_number.test_threshold

  condition:
    - condition: numeric_state
      entity_id: input_number.test_sensor
      below: input_number.test_threshold

  action:
    - service: light.turn_on
      target:
      entity_id: light.front_lights

☝️ In this example, it will trigger on either change and still only run the automation when the condition matches.

But considering the examples in your report, you can go one step further:

- alias: Test
  id: b9fd928a-8749-428c-92d2-1540da5cb424

  trigger:
    - platform: homeassistant
      event: start
    - platform: event
      event_type: automation_reloaded
    - platform: state
      entity_id: 
        - input_number.test_sensor
        - input_number.test_threshold

  action:
      - choose:
          - conditions:
             - condition: numeric_state
               entity_id: input_number.test_sensor
               below: input_number.test_threshold
            sequence:
              - service: light.turn_on
                target:
                  entity_id: light.front_lights
        default:
          - service: light.turn_off
            target:
              entity_id: light.front_lights

So, in this example, it triggers is either the sensor or the threshold changes and corrects the light state using a choose. So this combines the two automations into a single one.

The cool thing about this method, is that multiple things can trigger it, for example, the startup of Home Assistant or when automation is reloaded (e.g., when you changed something). This will correct the state of the front lights to be correct when you change something or after Home Assistant has been offline (and missed the trigger) as well.

The downside of this, is of course, you'll trigger stuff more often (which in general isn't a problem). If it is, the only solution is to add to use your original automation, but with an added trigger for the reverse of the entities.

@github-actions
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Oct 21, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Nov 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants