Trigger on number change

Apols, I know this should be simple, but again, i’ve spent ages trying to figure this and haven’t got it nailed yet.

I need to trigger an automation whenever a value increments over a whole number. Specifically, when electric cost ticks over to the next pound £.

I’ve tried this, but it doesn’t work;

  trigger:
  - platform: template
    value_template: "{{ (states('sensor.elec_cost_today') | round(0)) > 0 }}"

I thought this would be evaluated every time the sensor changed, but it only triggers on the brief occasions that the sensor goes unavailable.

And now, while typing this, I’ve realised that round(0) is the wrong thing anyway as 0.5 would be rounded to 1, arrgghhh, this should be easy and I just can’t see it.

Thank you,

Amanda

try this:

{{ states('sensor.elec_cost_today') | float % 1 == 0 }}

The template will be evaluated every time the sensor’s state changes, but your comparison is between the sensor’s state and a fixed value of “0”. Once the state value is over 0 the template is “true” and the automation will not trigger again until the template evaluates to “false” and then changes to “true” again.

This will only trigger when the sensor’s value changes to a whole number, but if the price change changes from 0.99 to 1.01 it would not trigger. The “to” state has to be exactly 1 (or 2,3,4…).

EDIT: Removed my proposed method because Petro’s below is so much better :slight_smile:

1 Like

I like this idea, this would make it work all the time

{{ states('sensor.elec_cost_today').replace('.','') | int(0) % 1 == 0 }}

EDIT: hmm, it might not if it goes from odd to odd.

1 Like

Ok, lets go with a normal state trigger and condition. This checks the previous state and ceilings it to the next highest whole number. If the after state is above, it will continue the automation.

trigger:
- platform: state
  entity_id: sensor.elec_cost_today
variables:
  continue: >
    {{ trigger | default(none) is not none and trigger.to_state is defined and trigger.from_state is defined }}
  before: >
    {{ trigger.from_state.state | float(0) if continue else 0 }}    
  after: >
    {{ trigger.to_state.state | float(0) if continue else 0 }}
  above: >
    {{ after >= before | round(0, 'ceil') }}
condition:
- condition: template
  value_template: "{{ continue and above }}"
action:
...
3 Likes

Thanks everyone.

I’ve come up with this other potential solution too;

Template sensor

      elec_cost_pounds:
        value_template: "{{ (states('sensor.elec_cost_today') | int) }}"

and automation trigger

  trigger:
  - platform: state
    entity_id: sensor.elec_cost_pounds

Any reason why that won’t work?

:slight_smile: Amanda

1 Like

yeah, I forgot to take that into account. I just assumed that it would go from .99 to 1.0

I saw this…

and missed this…

:crazy_face:

It looks like it should.

But then I thought mine above would work too…so maybe you should just ignore me. :wink:

It might work as long as you don’t mind that it will trigger on decrement as well as increment… also you will need to guard against triggering on restart by setting an availability in the template sensor and a default for the “int”.

1 Like

Cool thanks. The value never goes down, but it does reset to zero at midnight, but thats OK for me.

Default value for an int… I tried to find this in the docs a couple of weeks ago and never really got to the bottom of it. How do I state that??

int(<default>)

so something like:

int(1)

see this thread for more info:

Thanks, knew the int default was something simple.

My post 6 works fine btw.

:slight_smile:
Amanda

1 Like

Thanks, this worked for me, too!