Template: binary sensor and trigger.from_state availability

Hi everybody,

I’m trying to set up a binary sensor that takes in account the from_state and to_state of itself as a part of its logic. But apparently I cannot access to it.

Here a basic example of what I’d like to do:

template:
  - binary_sensor:
      - name: sensor_name        
        state:> 
           {% if trigger.from_state.state == 'on' 
                AND /* add some logic here on others sensors */ %}
             off
           {% if trigger.from_state.state == 'off' 
                AND /* add some other logic here on others sensors */ %}
             on
           {% endif %}

Am I missing something or I am completely off the mark?

Wouldn’t you need to be making a Trigger-based template binary sensor for the trigger.from_state to have any value/meaning?

Maybe take a look at the documentation for Template sensors.

Yeah, Id’ go with that solution, I thought those infos where still available in some ways, like this.from_state etc etc. I’ll look into the trigger-based templates.

thanks

No they are variables only available for triggers.

What exactly is this binary_sensor supposed to do?

I’d describe a binary sensor as an entity that has a state of on or off. The state will depend on any information that HA has available to it.

For example: you could have a binary sensor that is ‘on’ when the name of the current day has a ‘r’ in it ‘off’ otherwise.

I’m not sure if you replied to me but my question was intended for reaand. I asked what their binary_sensor is supposed to do (not what a binary_sensor does).

It’s unclear to me what’s their goal for a Template Binary Sensor that attempts to reference from/to states (which, as you pointed out, don’t exist other than in a Trigger-based Template Binary Sensor). At best, a Template Binary Sensor can reference its current state using this.state.

@reaand

What is it that you want this Template Binary Sensor to do?

Ok. Here what I’m trying to do (I have a couple of examples). I have a Samsung tv whose add-on doesn’t work as intended, so I’m trying to catch its status from a plug.

Here the logic I’m trying to implement:
off status: sensor.plug_power = 0, binary_sensor.tv_status = off
on status:
tv_status = off and sensor.plug_power > 60, binary_sensor.tv_status turns to on
off status:
tv_status = on and sensor.plug_power < 20, binary_sensor.tv_status turns to off

Here what I came up with:

    binary_sensor:
      - name: status_samsung_tv
        state: >
            {% set val = states('sensor.presa_tv_power')| default(0) | float %}
            {% if  trigger.from_state.state == 'on' and val>60 %}
            on
            {% elif val<20 and this.from_state== 'on'  %}
            off
            {% else %}
            {{ this.state }}
            {% endif %}
        attributes:
          trigger_id: >
            {{ trigger.id  }}
          from_state: >
            {{  this.state }}

It’s not working since it will always trigger, due to the sensor plug power nature, and it gets stuck on previous state = on and current state = on, so I’d like to skip updating the from_state when there’s no state change. (I may implement a check into the from_state, I will give it a try).

ETA:
mmm, not sure if I can access this.from_state or I can use this.from_state.state. Apparenty neither of them are working.

ETA2:
I can access to it with this.attributes.from_state.

Ok, this one is the current code
the issue is that the plug power keep triggering and so the from_state changes and I find the binary_sensor attribute from_state to be equal to its state.
Currently trying to prevent this to happen, and so to correct this I added or this.state=='on’ in the first if block.

  - trigger:
    - platform: state
      entity_id: sensor.presa_tv_power
      id: presa_tv
    binary_sensor:
      - name: status_samsung_tv
        state: >
            {% set val = states('sensor.presa_tv_power')| default(0) | float %}
            {% if val>50 and (this.attributes.from_state == 'off' or this.state=='on') %}
            on
            {% elif val<20 and (this.attributes.from_state == 'on')  %}
            off
            {% else %}
            {{  this.state }}
            {% endif %}
        attributes:
          trigger_id: >
            {{ trigger.id  }}
          trigger_value: >
            {{ trigger.from_state.state }}
          from_state: >
            {{  this.state }}

You are triggering on all attribute changes as well as state changes. To trigger on state changes only add a null to: to the trigger:

  - trigger:
    - platform: state
      entity_id: sensor.presa_tv_power
      to: 
      id: presa_tv

According to the logic you’re trying to implement, the Template Binary Sensor is off when power is less than 20 and on when power is greater than 60.

What should the Template Binary Sensor report if the power is between 20 and 60? Or can it never be within that range?

The fact it is the power that changes very often, like every 10 seconds, that’s the state of the sensor behaviour. Not of attributes.

That’s why I’m trying to use a from_state, depending on the from_state the binary_sensor behaviour should change. If the binary sensor is on then it should switch to off if the value goes below 20.
If the binary sensor is off it has to switch to on if the value goes above 60.
In-between it shouldn’t do anything. But it instead do something, it changes the from_state to the current state messing the whole logic.
What I need is to stop the trigger to fire once it switch to on if the power value doesn’t fall below 20 and vice-versa.
(While writing an idea came up, I could define 2 triggers on the same sensor … mmm, I will try).

Why not just use two Numeric State Triggers in the Triggered-based Template Sensor?

- trigger:
    - id: 'on'
      platform: numeric_state
      entity_id: sensor.presa_tv_power
      above: 50
    - id: 'off'
      platform: numeric_state
      entity_id: sensor.presa_tv_power
      below: 20
  binary_sensor:
    - name: Status Samsung TV
      state: "{{ trigger.id == 'on' }}"
1 Like

Not if the from state == the to state.

That’s so neat. This is what I was looking for, thanks!!
So, whenever the value is above (and stays above) the threshold it won’t trigger anymore.

1 Like

Hello I was looking for similar solution and feel this great! However one thing to improve is, when I reload the yaml or restart home assistant, the binary sensor will be “unknown” until next trigger. Is there any way to set an initial state of the binary sensor? Thanks!

After a restart, a Trigger-based Template Binary Sensor’s previous state is restored.

From the documentation:

The state, including attributes, of trigger-based sensors and binary sensors is restored when Home Assistant is restarted. The state of other trigger-based template entities is not restored.