Sensor with a trigger

Hello,

is it possible to create an ON/OFF sensor that goes to ON state by triggering when switch_1 goes to ON state ? It doesn’t have to go to OFF state when switch_1 goes to OFF state…

Yes it is possible using a template binary sensor:

binary_sensor:
  - platform: template
    sensors:
      switch_1:
        friendly_name: "Switch 1 sensor"
        value_template: >-
          {{ is_state('switch.switch_1', 'on') }}

The example above will create binary_sensor.switch_1 which will follow the switch on and off.

Can I ask why?

Hello Tom,

in your example, it seems that the sensor reflects the state of switch_1. So, if switch_1 is “off”, the sensor will be off too. Instead, I want that the sensor becomes ON when triggering the ON state, but DOESN’T HAVE to become OFF when triggering that state.
I want that because I want to emulate an alarm central, with an “alarm state indicator” that becomes ON when an alarm sensor becomes ON and doesn’t become OFF when the sensor becomes OFF

How will you turn it off then?

The main logic will be:

if triggered alarm_central == ON and ( door_contact == ON ) then alarm_state = TRUE;
if triggered alarm_central == OFF then alarm_state = FALSE;

if alarm == ON and door == ON then state = TRUE
if alarm == OFF then state = FALSE

So what should state be if alarm == ON and door == OFF?

Thanks for your answers. As said before, this is not the logic that I want. I try to show it again:

DEFAULT (NO TRIGGER): state = FALSE
IF TRIGGERED alarm_central == ON and door == ON then state = TRUE
IF TRIGGERED alarm_central == OFF then state = FALSE

Note the word “TRIGGER”

I don’t think you understand how template sensors work. I can create a template sensor that monitors the state of alarm_central. When alarm_central’s state changes, the template sensor will evaluate its template. So adding the word triggered to the pseudo code has little bearing on my original question. However, I’ll rephrase my question using the word triggered. What should state be in this case:
IF TRIGGERED alarm_central == ON and door == OFF then state = ?

Sorry. I think instead you did not’t understand my question. I know how template sensors work. It’s you that are insisting on template sensors, not me. I need an entity that is set to OFF as default and becomes ON or OFF based on trigger conditions.

Make an input Boolean for it

The requirement is to latch the state once the alarm is triggered. It can be an input_boolean but then it’ll need an automation to implement the latching behavior. A template binary sensor is self-contained and and can do the latching.

(deleted: meaningless question)

We’re discussing your pseudo-code. Whether it’s used in a template sensor or in an automation or in anything else, your pseudo code is incomplete.

It covers the case where alarm can be on or off but doesn’t cover the case when the door may be off.

You wrote “I don’t think you understand how template sensors work.”.
I did not say anything about template sensors, and you asked twice the same thing which has nothing to do with my question.

thanks, this seems to be the trick.

Note, in addition, that my pseudo code is not incomplete.

If so, then what should state be in this situation:
IF TRIGGERED alarm_central == ON and door == OFF then state = ?

Please, read again. The logic behind the situation you are describing is included in the words “default” and “trigger”. I expressed the logic clearly in the initial question:

“is it possible to create an ON/OFF sensor that goes to ON state by triggering when switch_1 goes to ON state ? It doesn’t have to go to OFF state when switch_1 goes to OFF state…”

“TRIGGER” is the keyword. Then I made a description wiith an alarm central, and I added a “default” word after, to be even more clear. When someone doesn’t understand a question it doesn’t mean that the question is wrong or incomplete.

It’s a simple question about your pseudo-code. The state of door can be either on or off. Your pseudo-code handles state=on. My simple question was what happens if the door’s state=off. It should be a simple answer but you haven’t provided it.

Tell you what, when you finally figure out how to achieve your goal, please post the solution. Then we can see how it implements your pseudo-code.

The answer was already both in the pseudo code (DEFAULT keyword) and in the initial question.