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?
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.
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.
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 }}
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).
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!
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.