Using variables in automations

Hello everybody
I’m trying to build up a very easy automation to check the voltage of a couple the batteries using variables, but I can get ride of it. Here the code

alias: Test1
description: ""'
   - platform: numeric_state
     entity_id: sensor.d1_pro_voltage
     below: '2.6'
        trigger_message: "D1 Battery low"
   - platform: numeric_state
     entity_id: sensor.mini_voltage
     below: '2.6'
       trigger_message: "Mini Battery low"
  - service: notify.mybot
       message: ' {{ trigger_message }} '
mode: single

Could you give me any lead? I have used the same structure for the state platform (with binary sensors) and works perfectly.

Your automation might not have proper indentation (which is critical for YAML) however no one can tell for sure because what you posted is not formatted correctly.

For more information, review guideline 11 in the FAQ.

In additions to the possible formatting issue…

Be aware that the automation does not “check” the voltage… For your numeric state trigger, the voltage has to change from above 2.6 to below 2.6 for the trigger to fire and the actions to be executed. If your batteries are already below 2.6v, they will have to go above 2.6v then back below it to trigger the automation.

I have tried to format correctly as the copy here isn’t as I wrote it :face_with_raised_eyebrow:

Yeah, I understand. To try the automation I set the threshold to 5 (now the battery is to 2.8) so it should trigger … doesn’t it?

No, that is functionally the same… Since the voltage is already below 5, it will have to go above 5 then back below 5 to trigger the automation.

You can test the trigger by manually altering the voltage value in the Developer Tools - States menu and clicking the “Set State” button.

For a real-world test, change it back to 2.6 and then you just have to wait until the device voltage drops.

I’ve got the point. Thank you very much, I will try it. I could insert a condition to test it … as the voltage value remains below 5 for 2 minutes let’s say. Should it trigger in that case?

It’s not the same. Please take the time to format the code. Read this.

It’s a Numeric State Trigger so the value has to cross the threshold. That means it must decrease from a value above 2.6 to a value below 2.6 to trigger.

Fires when the numeric value of an entity’s state (or attribute’s value if using the attribute property, or the calculated value if using the value_template property) crosses (and only when crossing) a given threshold.

Thank you to all. I understood the concept. I forced the value as Drew suggested and it worked. Nevertheless, I will read the text formatting document as the cut and paste doesn’t work as far as I’ve seen :grinning:

The purpose of the Solution tag is to mark the post that solved the original problem. You marked your own post but it contains no information to help users solve a similar problem.

The problem appears to be that you didn’t understand how a Numeric State Trigger works. Your post, the one you marked as the Solution, doesn’t explain how it works.

For more information, refer to guideline 21 in the FAQ.

1 Like