Help with defining a template

I have read HA instructions, read thru several threads herein, and got some great guidance from u/Didgeridrew, however, I’m still stuck on creating a couple seemingly simple template sensors.

  1. I wish to set pc1_session_state either ‘Locked’ or ‘Unlocked’, based on the state of ‘sensor.mqtt_sessionstate’. I already have an Automation checking for ‘sensor.mqtt_sessionstate’. Problem is, the Retain Flag does not work on HA reboot. So, I want to set another sensor once there is a change.
template:
  - sensor: 
      - name: PC1 Session State
        state: >
          {% if states('sensor.mqtt_sessionstate'), 'Unlocked' }
            Unlocked
          {% else %}
            Locked
          {% endif %}
  1. I would like to set a sensor based on Sleep state or not. Unlike the above ex, I’m not tracking an existing sensor. I have a Sleep Script and a Wake Script, looking to set ‘sleep’ or ‘wake’ respectively when executing these scripts. Reason… I want to use these as a variable in Conditions for other Automations, scripts, ie. prevent lights from coming on at Sunrise until I’m actually awake. Thoughts on this?

You’re missing a % at the end of the first set statement and your closing parentheses should be after the state you’re checking for.

For part 2 there are two main options.

  1. Use an input boolean helper. Have your scripts include a service call to turn the helper on or off as needed.
  2. Set up a trigger-based Template binary sensor. Have your scripts raise a custom event that triggers the sensor to render its template.

If you think you will need/want to manually override the state, the boolean helper way is easier.

Thank you! I did make the below change, as I only want it to trigger on Locked or Unlocked…

template:
  - sensor: 
      - name: PC1 Session State
        state: >
          {% if states('sensor.mqtt_sessionstate'), 'Unlocked' %}
            Unlocked
          {% elif states('sensor.mqtt_sessionstate') 'Locked' %}
            Locked
          {% else %}
          {% endif %}

In the other thread you helped with, you had the below. If I wanted instead to use it, how would it look like in my template?

   'not_to':
    - unknown
    - unavailable

For the other, I’ll check into the boolean helper.

You would add a trigger to the sensor configuration:

template:
  - trigger:
      - platform: state
        entity_id: sensor.mqtt_sessionstate
        not_to:
          - unknown
          - unavailable
    sensor: 
      - name: PC1 Session State
        state: >
          {% if states('sensor.mqtt_sessionstate'), 'Unlocked' %}
            Unlocked
          {% elif states('sensor.mqtt_sessionstate') 'Locked' %}
            Locked
          {% else %} Locked
          {% endif %}

For the PC session state, since you presented the trigger, I decided to go w/ that and removed my Automation…

- trigger:
  - platform: state
    entity_id: sensor.mqtt_sessionstate
    not_to:
      - unknown
      - unavailable
  sensor: 
    - name: PC1 Session State
      state: >
        {% if states('sensor.mqtt_sessionstate'), 'Unlocked' %}
          Unlocked
        {% else %}
          Locked
        {% endif %}

I realized, this still is not working… I’m still getting Unknown for my pc1_session_state sensor.
I think I needed to reboot HA one more time, as it seems to be working now.

I decided to try handling it this route. I’ve created the below, though I must have some aspect of the action wrong…

Template Binary Sensor:

  trigger:
    - platform: state
      entity_id: binary_sensor.sleep_state
      to: "on"

Script/Action/Manual Event:

{ 'entity_id': 'binary_sensor.sleep_state', 'event': 'sleep_state_changed' }

What I was proposing would be more like:

Script:

alias: Scott's sleep script
sequence:
  - #Other actions for sleep stuff
  - event: custom_set_sleep_sensor
    event_data:
      state: "on"

You would use the same event call in your “Wake” script, but change the value for state to ‘off’.

Trigger-based Template Binary sensor:

  - trigger:
      - platform: event
        event_type: custom_set_sleep_sensor
    binary_sensor:
      - name: Sleep State
        state: "{{ trigger.event.data.state }}"
1 Like

Put such things in dev tools as well. There is more missing/wrong than a single %

      {% if states('sensor.mqtt_sessionstate') == 'Unlocked' %}
        Unlocked
      {% elif states('sensor.mqtt_sessionstate') == 'Locked' %}
        Locked
      {% else %}
      {% endif %}

Thank you, though the template works as I have it w/o any errors, and no errors pop-up in the Template editor; it actually provides me the result.

When I place your rewrite into the Template editor, I receive an error about the ‘==’, and w/o those, I get the following error…

TemplateSyntaxError: expected token ‘end of statement block’, got 'string’

Don’t think so. Please post your code, which delivers the error about the ==

For my event, would the event_type then be… event_sleep_sensor?

and for the binary_sensor… binary_sensor.custom_set_sleep_sensor?

Though I appreciate it, the code u/Didgeridrew help me create passes in the Template editor and it works… All I need.

I had a copy/paste error, I have fixed it above.

The event type is custom_set_sleep_sensor.

The binary sensor’s entity ID is derived from the name value so it will be binary_sensor.sleep_state.

I’m still confused with this aspect of the Template…

binary_sensor:
      - name: Sleep State
        state: "{{ trigger.event.data.state }}"

I don’t follow what trigger.event.data.state is. I tried searching for this in the docs.

Also… for custom events, I did not know that actually adding ‘custom’ to the name was required.

it’s not required. Subscribe to your custom event, fire the event and look at the returned data, then you’ll see what trigger.event.data.state is. Keep in mind, that you’ll need to understand trigger templates, specifically event.

Thank you. Yep, all works as I wanted.

As Petro said it’s not. The custom event type can be whatever slug you want, I tend to start mine with custom_ just to make search easy. You just need to make sure the event type raised and the type consumed match exactly.

2 Likes

Thought I would come back to this and at least make your suggestions work… This is what I came up with that works. As for the previous error I was getting… I had not removed the comma’s.

# PC1 Session State
- trigger:
  - platform: state
    entity_id: sensor.mqtt_sessionstate
    not_to:
      - unknown
      - unavailable
  sensor: 
    - name: PC1 Session State
      state: >
        {% if states('sensor.mqtt_sessionstate') == 'Unlocked' %}
          Unlocked
        {% elif states('sensor.mqtt_sessionstate') == 'Locked' %}
          Locked
        {% else %}
        {% endif %}