Automation using the old_state of an entitiy

Hello,

I want to realize a notification based on the current power of a switch.
My working automation which triggers if the current power is below a threshold:

automation dryer: 
  trigger: 
    platform: state 
    entity_id: switch.dryer 
  condition: 
    platform: numeric_state 
    entity_id: switch.dryer 
    value_template: '{{ states.switch.dryer.attributes.current_power_mwh }}' 
    below: 100 
  action: 
    service: notify.mypushetta 
    data: 
      message: "Dryer stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}" 

But it triggers everytime the state changes and is below 100 mwh. What I really need is that it only triggers once when the old_state was above 100 mwh and the new_state is below 100 mwh. Even after hours of trying I did not find an easy way to do that.

A workaround would be to use a variable within a template - is that necessary or can someone give me a hint for an easy solution?

The numeric state trigger does exactly that

As balloob said, you should use numeric_state trigger:

automation dryer: 
  trigger: 
    platform: numeric_state 
    entity_id: switch.dryer 
    value_template: '{{ states.switch.dryer.attributes.current_power_mwh }}' 
    below: 100 
  action: 
    service: notify.mypushetta 
    data: 
      message: "Dryer stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}"

So once it gets below 100, action will be triggered.

Thanks for your replies!
I was trying to realize it with the numeric state before, but never got a message out of my rule. Copy paste from my configuration file:

automation dryer:
  trigger:
    platform: numeric_state
    entity_id: switch.dryer
    value_template: '{{ states.switch.dryer.attributes.current_power_mwh }}'
    below: 100
  action:
    service: notify.mypushetta
    data:
      message: "Dryer stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}"

One state change from yesterday where it should trigger, but (in contrast to the automation rule with the condition from my first post) did not result in a message using the numeric state:

16-04-24 12:43:33 INFO (ThreadPool Worker 9) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state switch.dryer=on; current_power_mwh=2890.0, friendly_name=dryer @ 2016-04-24T14:42:11.206536+02:00>, new_state=<state switch.dryer=on; current_power_mwh=40.0, friendly_name=dryer @ 2016-04-24T14:42:11.206536+02:00>, entity_id=switch.dryer>

My snippet is different than the one you wrote in the first post.

Sorry, I should have written it more clearly. The snippet in my latest post is from my configuration file and seems to be exactly the same you posted. I tested it yesterday and did not get a message from it.

Try using double quotes ( " ) at value_template and below, just to test.

value_template: "{{ states.switch.dryer.attributes.current_power_mwh }}"
below: "100"

Thanks for your reply, but it didn’t work :sweat:

Event:

16-04-26 05:46:03 INFO (ThreadPool Worker 5) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state switch.dryer=on; friendly_name=dryer, current_power_mwh=3080.0 @ 2016-04-26T07:44:41.327096+02:00>, new_state=<state switch.dryer=on; friendly_name=dryer, current_power_mwh=40.0 @ 2016-04-26T07:44:41.327096+02:00>, entity_id=switch.dryer>

Extract from my configuration file:

automation dryer:
  trigger:
    platform: numeric_state
    entity_id: switch.dryer
    value_template: "{{ states.switch.dryer.attributes.current_power_mwh }}"
    below: "100"
  action:
    service: notify.mypushetta
    data:
      message: "Dryer stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}"

automation dryer2:
  trigger:
    platform: state
    entity_id: switch.dryer
  condition:
    platform: numeric_state
    entity_id: switch.dryer
    value_template: '{{ states.switch.dryer.attributes.current_power_mwh }}'
    below: 100
  action:
    service: notify.mypushetta
    data:
      message: "Dryer 2 stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}"

I only receive the “Dryer 2 …” message. The change for double quotes in the “value_template” and “below” sections did not change that.

Any suggestions how to proceed?

Thanks in advance,
Daniel

If that’s an actual extract of your config, try joining that two into one automation tag only.

automation:
- alias: "dryer 1"
  trigger:
  ...
- alias: "dryer 2"
  trigger:
  ...

Descibing the automations as alias did not change the strange behaviour. I receive a message from “dryer 2” but not from “dryer 1”:

automation:
  - alias: "dryer1"
    trigger:
      platform: numeric_state
      entity_id: switch.dryer
      value_template: "{{ states.switch.dryer.attributes.current_power_mwh }}"
      below: 100
    action:
      service: notify.mypushetta
      data:
        message: "Dryer stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}"
  - alias: "dryer2"
    trigger:
      platform: state
      entity_id: switch.dryer
    condition:
      platform: numeric_state
      entity_id: switch.dryer
      value_template: '{{ states.switch.dryer.attributes.current_power_mwh }}'
      below: 100
    action:
      service: notify.mypushetta
      data:
        message: "Dryer 2 stopped because current power is {{ states.switch.dryer.attributes.current_power_mwh }}"

Implemented a workaround using a binary_sensor template which only triggers once:

binary_sensor:
  platform: template
  sensors:
    dryer_running:
      value_template: '{{ states.switch.dryer.attributes.current_power_mwh > 100}}'
      friendly_name: 'Dryer running'

automation:
  - alias: "dryer"
    trigger:
      platform: state
      entity_id: binary_sensor.dryer_running
      from: "on"
      to: "off"
    action:
      service: notify.mypushetta
      data:
        message: "Dryer stopped (current power: {{ states.switch.dryer.attributes.current_power_mwh }})"

I do not understand why the numeric_state triggers does not work, but the benefit of the workaround is that as addition to the push message there is now a visual feedback in the frontend visualising that the dryer is running.

Thanks for the help!

Hey
I have similar trouble using the numeric state trigger. I tried to work around with a template sensor, but that didn’t work out jet either. My guess is, that the numeric state trigger doesn’t work, because the return of the value_template is actually a string and not a number. Can this be or is that excluded?

Due to the fact, that the comparision in the value_template of the binary_sensor works (my last post), I conclude that it is a number and not a string.

But the code in the comparison is a string:

automation:
  - alias: "dryer"
    trigger:
      platform: state
      entity_id: binary_sensor.dryer_running
      from: "on"
      to: "off"

As far as I see it, the state changes from “on” to “off” and not from 1 to 0.

Edit:
Ah now I see what you mean, you meant the: [quote=“byte-bender, post:11, topic:804”]
value_template: ‘{{ states.switch.dryer.attributes.current_power_mwh > 100}}’
[/quote]
Anyhow I think this are two different situations. Here we have a value_template and the comparison is done within the value_template. This works yep, but what is not working, is

automation:
  - alias: "dryer1"
    trigger:
      platform: numeric_state
      entity_id: switch.dryer
      value_template: "{{ states.switch.dryer.attributes.current_power_mwh }}"
      below: 100
      ...

I think, here HA evaluates the value_template first and then in a second step does the comparison. This then does not work, because now it checks if the return of the value_template is above 100 or not, but the return is a string and a string cant be compared to a number.

This might be wrong, its just what I think is happening.

If opened a ticket in the issue tracker numeric_state trigger with attribtes · Issue #2065 · home-assistant/core · GitHub. Lets see what the others say.