Automation if-then if new value is lower than current value plus large value change

Hi There, I have a weight sensor (kgs) and a helper (0-100%). I have an automation that runs ever 15mins to update the helper based on the sensor. Typically the weight changes only by 1-3kg at a time, definitely no more than 5kg.

Im getting better at templating, but I very much need a hand with this one please :slight_smile:

What I would like todo (separately):

1). If the new value for the helper (e.g.80%) is 10kg or higher than the current value if the helper (e.g. 30%), run an action

2). If the new value for the helper (e.g.35%) is lower than the current value (e.g.37%), run an action

The reason I want to run an automation is to trigger other actions based on the above outcomes.

You don’t need the helper and you should trigger on any state change of the weight sensor rather than periodically.

trigger: 
  - platform: state
    entity_id: sensor.your_weight_sensor
    to:
conditions:
  - condition: template
    value_template: "{{ trigger.to_state.state|is_numeric and trigger.from_state.state|is_numeric }}"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state|float - trigger.from_state.state|float >= 10 }}"
        sequence:
          - service: # (your +10kg actions here)

      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"
        sequence:
          - service: # (your decrease in weight actions here)
1 Like

Thanks @tom_l - more new stuff! Thanks :slight_smile:

Am i using this correct? as I get a Message malformed: extra keys not allowed @ data[‘conditions’] error in the automation editor. added the stop just for testing.

Note: sensor.rockgas_bottle_1_weight is currently “35.832”

trigger:
  - platform: state
    entity_id: sensor.rockgas_bottle_1_weight
    to: 
conditions:
  - condition: template
    value_template: "{{ trigger.to_state.state|is_numeric and trigger.from_state.state|is_numeric }}"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state|float - trigger.from_state.state|float >= 10 }}"
        sequence:
          - stop: "+10kg action"
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"
        sequence:
          - stop: "decrease in weight actions"

My bad. The main condition block should be:

condition:
  - condition: template
    value_template: "{{ trigger.to_state.state|is_numeric and trigger.from_state.state|is_numeric }}"

No worries… still getting an error tho sorry “Message malformed: invalid template (TemplateAssertionError: No filter named ‘is_numeric’.) for dictionary value @ data[‘condition’][0][‘value_template’]”

trigger:
  - platform: state
    entity_id: sensor.rockgas_bottle_1_weight
    to: 
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state|is_numeric and trigger.from_state.state|is_numeric }}"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state|float - trigger.from_state.state|float >= 10 }}"
        sequence:
          - stop: "+10kg action"
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"
        sequence:
          - stop: "decrease in weight actions"

Should be is_number

condition:
  - condition: template
    value_template: "{{ trigger.to_state.state|is_number and trigger.from_state.state|is_number }}"
1 Like

Perfect thanks!