Value_template issue

Hi, I have an automation for my car charger that used to trigger on this value template, but it’s not woking anymore:

    trigger:
      platform: template
      value_template: "{{ states('sensor.evse_mileage')|int > states('input_number.evse_max_charge_distance')|int }}"

The states of the entities used are:


and:

Where the first off course starts with 0 and should trigger this value template at 10, but it doesn’t.

What Am I doing wrong?

Cheers,

Michel

This looks OK so maybe the automation trigger is looking for a change to “true” when it is already “true”.

Try setting the input_number to 14 then back to 10 to see what happens, you can also monitor what is happening by pasting the template (everthing between and including the curly brackets) into Developer Tools - Template.

I have just followed your advise and checked the template. I was not aware that option was even available :wink:

When the states are like above, the value template is ‘true’ and when the charged milage is 0, that value template is false, so that seems to be ok.

Still it doesn’t work :confused:

This is the automation:

  ## Turn off at set charged milage
  - id: evse_uitschakelen_indien_bijgeladen_afstand_behaald
    alias: EVSE uitschakelen indien bijgeladen afstand behaald
    trigger:
      - platform: template
        value_template: "{{ states('sensor.evse_mileage')|int > states('input_number.evse_max_charge_distance')|int }}"
    action:
      - service: switch.turn_off
        entity_id: switch.evse

Maybe there is a better way of doing this. I’m just looking to stop the charge if the sensor state is larger then the input_number.

Tried:

  ## Turn off at set charged milage
  - id: evse_uitschakelen_indien_bijgeladen_afstand_behaald
    alias: EVSE uitschakelen indien bijgeladen afstand behaald
    trigger:
      - platform: numeric_state
        entity_id: sensor.evse_mileage
        above: "{{ states('input_number.evse_max_charge_distance')|int }}"
    action:
      - service: switch.turn_off
        entity_id: switch.evse

ahrr…: Invalid config for [automation]: expected float for dictionary value @ data[‘above’]. Got None. (See ?, line ?).

Ah ok thanks. Any idea why my other automation fails?

If I trigger it in the developer tools using the automation.trigger, it returns ‘false’ if the sensor.evse_mileage is ‘0’ and it returns ‘true’ when I set the sensor.evse_mileage to ‘20’ manually and then trigger the automation.

If I use the automation.trigger like so:

service: automation.trigger
data: {}
target:
  entity_id: automation.evse_uitschakelen_indien_bijgeladen_afstand_behaald

the evse switches off.

But still the automation won’t work by itself.

Thanks.

The automation is now configured as such:

  ## Turn off at set charged milage
  - id: evse_uitschakelen_indien_bijgeladen_afstand_behaald
    alias: EVSE uitschakelen indien bijgeladen afstand behaald
    trace:
      stored_traces: 1
    trigger:
      - platform: template
        value_template: "{{ states('sensor.evse_mileage')|int > states('input_number.evse_max_charge_distance')|int }}"
    action:
      - service: switch.turn_off
        entity_id: switch.evse

If I set sensor.evse_mileage from 0 to 55 manually this:

{{ states('sensor.evse_mileage')|int > states('input_number.evse_max_charge_distance')|int }}

returns ’ true’ in Developer Tools - template

and if I then chack the logs the automation has not been fired so it leaves me no log data.

I have now changed the automation to:

  ## Turn off at set charged milage
  - id: evse_uitschakelen_indien_bijgeladen_afstand_behaald
    alias: EVSE uitschakelen indien bijgeladen afstand behaald
    trigger:
      - platform: state
        entity_id:
          - sensor.evse_mileage
    condition:
      - condition: template
        value_template: "{{ states('sensor.evse_mileage')|int > states('input_number.evse_max_charge_distance')|int }}"
    action:
      - service: switch.turn_off
        entity_id: switch.evse

And now it works just fine. I’m confused, but happy it finally works again.
Thanks for helping me learn more about debugging automations!

Triggers only trigger if the template code changes from false to true. It wasn’t triggering because it was already true. True to true will not cause a trigger.

True, I didn’t think of hat. The evse is then already turned off, so I really can’t hurt, but its unnecessary, you are right.

As both automations are sub-optimal at best, what would be best to do in that case then?

Use the original automation and be patient. Wait for the sensor to drop below 10 and then raise above 10.

I tried as described above, but it didn’t work. And even if it does, it needs to switch off in all cases above the set mileage and in that case there would still be a potential issues that the evse does not reset and the milage stays above 10 and rising, without the evse ever switching off.

Would this be a better option?

  ## Turn off at set charged milage
  - id: evse_uitschakelen_indien_bijgeladen_afstand_behaald
    alias: EVSE uitschakelen indien bijgeladen afstand behaald
    initial_state: on
    trigger:
      - platform: state
        entity_id:
          - sensor.evse_mileage
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: switch.evse
        state: 'on'
      - condition: template
        value_template: "{{ states('sensor.evse_mileage')|int > states('input_number.evse_max_charge_distance')|int }}"
    action:
      - service: switch.turn_off
        entity_id: switch.evse

Blockquote
Your second example will also turn off the switch when mileage exceeds distance but it will continue to turn it off repeatedly with each increase in mileage . I don’t know if you are aware of that.

Is that so? I think I’m solving a ‘non issue’ as the sensor.evse_mileage state only changes when charging and thus does not trigger the automation if the charger is off (as the mileage does not change then)… correct?

There is no better option. If it needs to turn off for every state change above 10 then use whatever you want that works

1 Like

The mileage is usually set to 0 or ’ unknown’ when the charger is eiher reset or the car is unplugged from it, but there sometimes it some strange behaviour when wifi is lost or the disconnect is not properly detected.

Blockquote
It is so.
If repeatedly turning off the switch is something you want, now you have it.

Could you elaborate how that can happen? Maybe I understand this wrong…

The charger starts charging and ‘sensor.evse_mileage’ goes up from unknown to 3, 5, 9 get’s triggered, but the condition prevents it from switching the evse off… and
then finally it reaches the ‘input_number.evse_max_charge_distance’ (now set to 10) and then switches the evse off.

Since the evse is off, ‘sensor.evse_mileage’ will not go up any further and thus not trigger the automation any longer or am I still making a mistake and if yes what mistake exactly?