Struggling with value_template on a trigger

I am attempting to create an automation that picks up when the washing machine wattage goes over 10

im trying to use the value_template as per the automation trigger docs

trigger:
  entity_id: sensor.tumble_dryer_power
  platform: state
  value_template: "{{ sensor.tumble_dryer_power.replace(' W','')|float }}"
  to: 'above: 10'

im constantly getting the following error:

Message malformed: extra keys not allowed @ data['trigger'][0]['value_template']

ive tried changing sensor. to state. within the value_template but this seems to make no difference to the error I receive

any help appreciated as always (still only in my second week with HA)

1 Like

it won’t work because you’re “marrying” state and template triggers :wink:
read the docs more carefully :wink:

the approach really depends on what you want to achieve.
if your sensor.tumble_dryer_power returns a number (it’s a string but if it’s a valid number like ‘12’) you can use state trigger + template condition :wink:

- alias: bbb
  trigger:
    platform: state
    entity_id: sensor.tumble_dryer_power
  condition:
    condition: template
    value_template: >
      {{
        trigger.to_state and
        trigger.to_state.state and
        trigger.to_state.state not in ['unknown', 'unavailable'] and
        and trigger.to_state.state|int > 10
      }}
  action:

but it will perform action when sensor.tumble_dryer_power is 11, 12, 13 etc.
If you need it to run only once, use numeric state trigger

ahh ok, I should have set the value as a condition, seems pretty obvious now

however my sensor returns “1500 W” (as an example) so isnt a numeric value due to W at the end which im trying to remove with my original value_template

Thanks

Question: Is trigger.to_state guaranteed because the trigger time is ‘platform: state’?

Or is this just saying “these should be available…maybe”?

Likewise, the state object will always have and entity_id, a state, and a timestamp.

So checking if those are not null seems redundant in this case?

I think the answer is “depends”. For example, when HA is shutting down.

1 Like

You’re close with your template…but not quite.

value_template: "{{ sensor.tumble_dryer_power.replace(' W','')|float }}"

sensor.tumble_dryer_power doesn’t exist. You would have wanted states.sensor.tumble_dryer_power

But even if you had it right, this would be a state object, not a string.

states.tumble_dryer_power.state is the actual state. Or, states(‘sensor.tumble_dryer_power’) would be better in the event that sensor doesn’t exist (would return ‘unavailable’).

trigger:
  platform: template
  # If the conversion to float fails, this will return 0.0 and will be false.
  value_template: "{{ states('sensor.tumble_dryer_power').replace(' W','')|float > 10.0 }}"

create a template sensor that returns only a number (exactly that value_template) and use it in your automations.
btw, is it a custom sensor?

ok this makes no sense to me, Ive been back over the documentation and it appears what I am trying to do is correct.

it makes no sense to constantly trigger on the state of the device and then add a condition, when the UI itself gives me the option to trigger above and below a numeric value

really all im trying to do is remove the ’ W’ from the end of the string to use a numeric function on it, is it really this difficult or am I missing something obvious?

Cheers for the replies

its a Teckin SP23 flashed with tasmota, added to configuration.yaml manually

  trigger:
    platform: template
    value_template: "{{ states('sensor.tumble_dryer_power')[:-2] | int > 10 }}"

It lops off the last two characters of whatever value is returned by states('sensor.tumble_dryer_power'), converts it to an integer, then checks if it’s greater than 10.

and it will trigger only once when the value exceeds 10 and won’t trigger until it falls to 10 or below and then again exceeds 10.
adding this note as the TS did not tell if he’s happy with such a scenario.

Cheers guys, yeah im happy with the solution to just trigger above 10 as I will just make another automation for less then 10 or null as I want different actions to happen dependant

Sorry, what have I missed here ? Why not use the numeric state trigger ?

From the docs : -

automation:
  trigger:
    platform: numeric_state
    entity_id: sensor.temperature
    # Optional
    value_template: "{{ state.attributes.battery }}"
    # At least one of the following required
    above: 17

You can even dispense with the template (and no need far a condition at all) cleaner, simpler, less processor intensive

In the original template, it removes a trailing " W" from the state’s value. That implies it’s not just a pure number. I don’t think the Numeric State Trigger works with a value like "12.5 W".

Sorry, missed that ! Yeah to ONLY BEST way to process that is as you did with a template trigger which comes as close as possible to the numeric state.

I must buy new reading glasses :rofl: