I thought I’d take a shot at turning two automations into one by using the new automation action choose
. First, it isn’t listed in the interface, so I wrote this automation manually:
- id: 'freezer_automation'
alias: Freezer
description: Run the freezer during the day when solar power may be available
trigger:
- above: '9'
entity_id: sun.sun
platform: numeric_state
value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
- below: '12'
entity_id: sun.sun
platform: numeric_state
value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
condition: []
action:
- choose:
- conditions:
- condition: numeric_state
entity_id: sun.sun
value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
above: 9
sequence:
- service: switch.turn_on
entity_id: switch.sonoff_1_relay
- conditions:
- condition: numeric_state
entity_id: sun.sun
value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
below: 12
sequence:
- service: switch.turn_off
entity_id: switch.sonoff_1_relay
mode: single
It worked this morning to turn on the relay. It also shows up as having triggered in the evening, however, the relay was never turned off. The code looks good to me. Any thoughts? Back to two separate automations? Thanks in advance!
What does home-assistant.log say about what happened?
EDIT: Wait! 12 is above 9. Think about it. You need to use the trigger
variable.
1 Like
Try this:
- id: 'freezer_automation'
alias: Freezer
description: Run the freezer during the day when solar power may be available
trigger:
- above: '9'
entity_id: sun.sun
platform: numeric_state
value_template: '{{ state.attributes.elevation }}'
- below: '12'
entity_id: sun.sun
platform: numeric_state
value_template: '{{ state.attributes.elevation }}'
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.above == 9 }}"
sequence:
- service: switch.turn_on
entity_id: switch.sonoff_1_relay
default:
- service: switch.turn_off
entity_id: switch.sonoff_1_relay
mode: single
Thank you so much Phil! I spent so much time trying to figure this out but your hint and recommended solution totally cleared things up for me. I’ll give it a test today. Thanks again for your fresh pair of eyes and going a step further with some proposed code!
1 Like
FYI, this part:
- condition: template
value_template: "{{ trigger.above == 9 }}"
will work as is, but really should be:
- condition: template
value_template: "{{ trigger.above is not none }}"
I didn’t say this right away because I wanted to keep it a simple as possible. But, doing it this way will allow you to change the above value in the trigger and not have to worry about making the same change in the condition to make sure they match. As long as the trigger with the above fires, then this condition will be true.
1 Like
Thanks again! I’ll apply that to other automations too to make everything more maintainable and D.R.Y.