I have a trigger based template sensor configured to update its state whenever the device trigger is triggered.
However what I want to do is in the state template every time the device trigger is triggered the state of the trigger based template should cycle between a few states such as: “a”, “b”, or “c”.
How can I do this?
I used an if else statement but it didn’t work
- trigger:
- platform: device
domain: mqtt
device_id: 9ab0a61f9b8564a64c5aca0e833ee399
type: action
subtype: off_press_release
discovery_id: 0x001788010926c3d7 action_off_press_release
sensor:
- name: "Testing"
state: >-
'{% if is_state('sensor.testing', 'both') %}'
"left"
'{% if is_state('sensor.testing', 'left') %}'
"right"
'{% else %}'
"both"
I think my templating skills are the problem… I never know where to add the double or single quotes
Although it still didn’t work after revising it as below
- trigger:
- platform: device
domain: mqtt
device_id: 9ab0a61f9b8564a64c5aca0e833ee399
type: action
subtype: off_press_release
discovery_id: 0x001788010926c3d7 action_off_press_release
sensor:
- name: "Testing"
state: >-
{% if is_state('sensor.testing', 'both') %}
left
{% if is_state('sensor.testing', 'left') %}
right
{% else %}
both
The problem may be because I’m calling itself in a recursive way but this is because I want it to have an initial state AND have the sensor follow a state change pattern
Update:
Got it figured out.
- trigger:
- platform: device
domain: mqtt
device_id: 9ab0a61f9b8564a64c5aca0e833ee399
type: action
subtype: off_press_release
discovery_id: 0x001788010926c3d7 action_off_press_release
- platform: homeassistant
event: start
sensor:
- name: "Testing"
state: >-
{% if is_state('sensor.testing', 'both') %}
left
{% elif is_state('sensor.testing', 'left') %}
right
{% else %}
both
{% endif %}
HOWEVER if you reload just the template config it goes to unknown as it’s initial state. Just need to figure out what event occurs when you reload a config
DONE
- trigger:
- platform: device
domain: mqtt
device_id: 9ab0a61f9b8564a64c5aca0e833ee399
type: action
subtype: off_press_release
discovery_id: 0x001788010926c3d7 action_off_press_release
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
sensor:
- name: "Testing"
state: >-
{% if is_state('sensor.testing', 'both') %}
left
{% elif is_state('sensor.testing', 'left') %}
right
{% else %}
both
{% endif %}