Create template sensor with datetime when input_boolean have been turned off (last state)

Hi guys,
is it possible to create a template sensor to show the date time when an input_boolean have been turned off?

Thank you,
regards

Cristian

To set an actual input_datetime

trigger:
  platform: state
  entity_id: input_boolean.your_input_boolean
action:
  - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.your_input_datetime
      date: >
        {{ now().timestamp() | timestamp_custom("%Y-%m-%d", true) }}
      time: >
        {{ now().timestamp() | timestamp_custom("%H:%M:%S", true) }}

Or if you just want to display it in the front end, use the Lovelace secondary info option of the entities card:

Hi Tom,
tanks for the quick reply :slight_smile:

Maybe I’m not explain the request very well; my goal would be to have a sensor called “last turned off” that displays the datetime (e.g. 2019-02-15 12:15:22) when an input_boolean is switched off.

I defined this sensor:

  last_irrigated_time:
    friendly_name: "Last irrigation cycle ended"
    value_template: >
      {% if is_state('input_boolean.cycle1_running', 'off') %}
         {{ as_timestamp(states.sensor.cycle1_running_turned_off.last_updated) | timestamp_custom("%a %d %h at %H:%M") }}
      {% endif %}
    icon_template: mdi:update

… but when I turn on input_boolean.cycle1_running the sensor displays blank label.

in lovelace place the date sensor on a conditional card, and in the conditional card use the input_boolean as the driving factor for the display.

For your exact condition to show the last changed in a card

- type: conditional
  conditions:
    - entity: input_boolean.cycle1_running
      state: 'off'
  card:
    type entities
    entities:
      - entity: sensor.cycle1_running_turned_off
        secondary_info: last-changed

If you go the route mentioned above, the sensor will persist at all times with an empty value. From what I can tell you don’t want to see the sensor at all. This would be the only way to do it in Lovelace.

Not sure if I’m thinking to complicated here, but I do this with an input_datetime.

input_datetime:
  front_door_last_opened:
    name: Front Door Last Opened
    has_date: true
    has_time: true

And then I use an automation to push the date & time when the door is opened:

- alias: Door Last Opened Date and Time
  trigger:
    platform: state
    entity_id: binary_sensor.front_door
    from: 'off'
    to: 'on'
  action:
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.front_door_last_opened
        time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'

Actually, I have more than a dozen of these binary_sensors and use a template to be able to work them all with only one automation because the binary_sensors are called binary_sensor.NAME and the input_datetimes are all called input_datetime.NAME_last_opened.

The entity_id line I use is:
entity_id: input_datetime.{{ trigger.entity_id[14:] }}_last_opened

Thank you all guys :slight_smile:

Eventually I got it work; below my automation:

  • alias: Irrigation Cycle ended Date and Time
    initial_state: true
    trigger:
    platform: state
    entity_id: input_boolean.cycle1_running
    from: ‘on’
    to: ‘off’
    action:
    • service: input_datetime.set_datetime
      data_template:
      entity_id: input_datetime.cycle1_ended
      time: ‘{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}’
      date: ‘{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}’

I still had issues because the automation did not trigger so checking some of your old posts and I added ‘initial_state: true’, it did the trick, now it works perfectly; now I have just to assign the input_datetime value to the template sensor and hide the input_datetime.

Thanks again,
cheers

Cristian