Trouble with getting a boolean from a script in an automation

I have an automation that runs when a sensor is updated. It performs a bunch of analysis and then returns a structure with a scene and a boolean (whether or not to activate the scene).

When I paste my script into the template tool it evaluates, as I expect it to, to:

{
  "scene": "scene.high_power",
  "update": true
}

But I can’t get the conditional in my automation to use the value of update.

alias: Power management scenes
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.heuristic_power_1000_sec
conditions: []
actions:
  - action: script.scene_selector_script
    metadata: {}
    data: {}
    response_variable: new_scene
  - if:
      - condition: template
        value_template: "{{ new_scene.update }}"
    then:
      - action: scene.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: "{{ new_scene.scene }}"
mode: single

When I do a trace, it comes back that the conditional was false

Executed: January 17, 2025 at 1:44:28 PM
if
Executed: January 17, 2025 at 1:44:28 PM
Result:
result: false
if/condition/0
[If template renders a value equal to true]
Executed: January 17, 2025 at 1:44:28 PM
Result:
result: false
entities: []

Any ideas what I am doing wrong?

What does the “Changed Variables” section of the trace show for new_scene?

I hadn’t even noticed that feature before, thank you!

Here’s what it says (the state in my house has changed; the conditional is still true, but being evaluated as false)

context:
  id: 01JHTTKG6CJDD7EMG7NK7808V3
  parent_id: 01JHTTKG6B9THZ81190RKWVJJR
  user_id: null
new_scene:
  scene: scene.save_power
  update: true

I tend to make sure in cases like this that what I think is happening, is actually happening.
I would cast that as a bool…

      - condition: template
        value_template: "{{ new_scene.update | bool }}"

I tried that and now I get this error in my trace:

Error: In 'template' condition: ValueError: Template error: bool got invalid input '' when rendering template '{{ new_scene.update | bool }}' but no default was specified

Am I correct that this makes it look like a scope issue? like my value_template doesn’t know about new_scene?

Interesting, that might be a clue as well.

As a testing step I removed my conditional statement, and the scene was applied as expected.

This isn’t viable as a solution, I still need the conditional, but maybe the information helps?

actions:
  - action: script.scene_selector_script
    metadata: {}
    data: {}
    response_variable: new_scene
#  - if:
#      - condition: template
#        value_template: "{{ new_scene.update }}"
#    then:
#      - action: scene.turn_on
#        metadata: {}
#        data: {}
#        target:
#          entity_id: "{{ new_scene.scene }}"
  - action: scene.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: "{{ new_scene.scene }}"

Try adding the condition directly to the action block:

...
actions:
  - action: script.scene_selector_script
    metadata: {}
    data: {}
    response_variable: new_scene
  - condition: template
    value_template: "{{ new_scene.update }}"
  - action: scene.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: "{{ new_scene.scene }}"

I gave that a try…

I got the same behavior.

trace results for changed variables from my script:

context:
  id: 01JHV13T57X5W12WV27REYV839
  parent_id: 01JHV13T569PK5SQGVAPNG00MX
  user_id: null
new_scene:
  update: true
  scene: scene.low_power

And the conditional step

@Sir_Goodenough @Didgeridrew

Thank you both so much for helping me with this. I finally got it working.

I think update must be some kind of reserved keyword or something. I changed the name of the variable from ‘update’ to ‘doit’ and everything started working just as I intended.

context:
  id: 01JHV4JTJJ0FHGY05HMG7PMHS1
  parent_id: 01JHV4JTJJKXVAXY9ZSHH33Z7G
  user_id: null
new_scene:
  doit: 'true'
  scene: scene.save_power

2 Likes

That is strange, it must be clashing with an underlying Python method… I just checked and it does work if you use bracket notation instead of dot notation as follows:

  - if:
      - condition: template
        value_template: "{{ new_scene['update'] }}"
2 Likes

wild, I didn’t even know there was an alternative to the dot notation.

(I’ve only been doing home assistant for about 3 weeks)