Automation based on change in sensor

I’m trying to do something that should be fairly simple but I’m doing something wrong. Basically I want to vary my ventilation speed depending on the outside particle count so I can avoid smoke getting in. I’ve had HA for a while but I haven’t done anything advanced so feel free to explain things to me like I’m Michael Scott trying to understand surplus.

I can of course read the number and make automations based on that but I want to check for sudden spikes too - change in from_state to to_state (I also have the derived sensor which might be useful). And after that I would like to have some average of last x readings as well but one step at a time…

First thing I tried was just making a trigger for state change and read to_state - from_state but I couldn’t get that to work. Then I read that perhaps the change between reading should be a template sensor and I tried to build one but again with zero success.

Is template the way to go and what should it look like?

NB: I do programming for a living and feel like I should be able to understand the structure better but somehow I’m just not getting it…

1 Like

Tom’s suggestion is a good one, but…

…if you show us what you tried we can help there too. My guess is you don’t know that states are strings (attributes retain their types though), so you need to cast it first.

I know I should supply code but I’ve been through a number of trials. Here’s my current attempts:

The template has this:

trigger:
  -platform: state
  entity_id: sensor.ikea_of_sweden_vindstyrka_particulate_matter
action:
  - service: input_number.set_value
  target:
    entity_id.input_number.pm25prevdiff
  data:
    value_template: >-
      {% set fromvalue = trigger.from_state | int %}
      {% set tovalue = trigger.to_state | int %}
      {{tovalue - fromvalue}}
mode: single

And I tried updating a simple integer value on state change like this (neither works):

service: input_number.set_value
target:
  entity_id: input_number.pm25prevdiff
data:
  value: "{{ trigger.to_state| int }} - {{ trigger.from_state | int }}"

On both I had “.state” after to_state/from_state but that didn’t make a difference.

You definitely need it to be, for example:

trigger.to_state.state | int(0)

to_state and from_state are both state objects.

What does the history of you sensor look like?

The sensor gives off readings just fine. I even have this automation to save the previous reading and that works fine

service: input_number.set_value
target:
  entity_id: input_number.prevpm25
data:
  value: "{{ trigger.from_state.state | int }}"

Now that I’m in front of a computer:

value: "{{ trigger.to_state.state | int(0) - trigger.from_state.state | int(0) }}"

You need to put everything inside a single print statement. If you split it like you had, you just have strings again. In fact, I think you would have an error in your log using the {{ ... }} construct twice.

1 Like

That was at least part of the solution. The number for difference between from_state and to_state gets updated.

1 Like