Automation condition always evaluates to true, it seems

hi

i’m a newbie, but i’m a developer in other walks of life.

my house occasionally gets power cuts, like most people’s, and my deconz doesnt connect on a system startup nicely until homeassistant itself is rebooted.

so i’ve come up with this script.

- id: '1597444444444'
  alias: HomeAssistantRebootAfterPowerOn
  description: 'DeConzNeedsRestartOfHA'
  trigger:
    minutes: '/1'
    platform: time_pattern
  condition: 
  - condition: template
    value_template: "{{ (states.sensor.system_up_time_seconds.state < '250') }}"
  action:
  - data: {}
    service: homeassistant.restart
  mode: single

(i’ve put my code inside back ticks - why does it still pop up at me? when i post this. )

so i left it running, and i tested it by turning the rPi off , and on.

and it did what it was supposed to do. so i waked away for a few hours. came back.

but … it still was rebooting for some reason. i disabled the whole automation temporarily, and the homeassistant stopped rebooting (so i know it is this script). Re-Enabled the script, and sure enough it started rebooting HA again.

the sensor.system_up_time_seconds was well above 250, it was in the thousands (26322). but still it got past the condition.

so i’m baffled really. is it evaluating the condition as ASCII rather than a number. how do i force it to use integers.

also, separate questions, when i open those brackets in yaml… {{ am i then writing in jinja2 language? just wondered.

thanks

That’s likely your issue, you’re comparing it to a string

Of course, there’s a numeric state condition that’d handle this cleanly for you. I’m not sure why you’re not simply using that?

  condition: 
  - condition: numeric_state
    entity_id: sensor.system_up_time_seconds
    below: 250
1 Like

thanks for your reply…

value_template: "{{ (states.sensor.system_up_time_seconds.state < 250) }}"

i’ve modified the above… just taking away the apostrophes surrounding the 250. does this make it think i’m comparing numbers now then?

i’m not an expert on this syntax. i keep seeing examples where they type " | int " in the middle of conditional statements, and i dont know what that is meant to mean. if i interpret it in c++ or javascript , in my mind, it makes no sense to me in that world.

the reason i’m not using the “simpler” numeric state condition… ultimately…, is because i’d like a unified method of defining such conditional statements (instead of sometimes data and sometimes data_template, indent this, dont indent that). there seems to be 3 different ways of doing things in hass. and i’d like to just learn one, the most complicated way, because ultimately i’m going to be doing some complicated conditional statements later, and condition: -condition: [] isn’t quite going to cut it. i’m happy to work in a programming form, and space things out, and #comment things.

so is this {{ }} notation making it interpret the inner working of those curly brackets in the jinja2 language?

thanks again

alsoo… i’ve now changed it to

- id: '1597968436890'
  alias: HomeAssistantRebootAfterPowerOn
  description: 'DeConzNeedsRestartOfHomeAssistantAfterSystemPowerOn'
  trigger:
    minutes: '/1'
    platform: time_pattern
  condition: 
  - condition: template
    value_template: "{{ (states.sensor.system_up_time_seconds.state < 250) }}"
#  condition:
#  - above: '10'
#    below: '250'
#    condition: numeric_state
#    entity_id: sensor.system_up_time_seconds
  action:
  - data: {}
    service: homeassistant.restart
#  - data:
#     message: You are on your way home2.
#     title: Automation
#    service_template: notify.mobile_app_mi_9
  mode: single

and restarted the whole rPi system.

the variable “system_up_time_seconds” is 63, 123, 183 (as time goes by) and the condition never evaluates to true and thus the hass does not reboot.

you’ll notice the #commented out bits (which use Numeric State in yaml speak) - this does work! (in
a separate test) but why doesn’t my Jinja2 syntax work in this test?

thanks

i’ve solved it anyway using the below code in Jinja2

    value_template: "{{ states('sensor.system_up_time_seconds') | int < 250 }}"

for anyone who is interested. the " | int " portion casts the preceding parenthesis value from string to integer, forcing a good comparison.

thanks for your help

data_template is going away - so that’s one less thing to worry about :wink: Indenting will always be a thing to worry about while you use YAML though. The only “solution” is to jump ship to Node Red.

Yes, and that’s shown (but not explained) in the templating docs. It is explained in the linked Jinja2 docs though, since it’s a Jinja2 thing.