How to copy/use a time from an entity state?

The automation I’m trying to create would look like this:

Next alarm time is a state inside the alexa entity. I want to grab this time and use it as a condition for other automations. Like if the alexa alarm is set for 5a, do something at 5:15a. I do want to save the time in a separate variable, so I can still use that time even after the alarm is cancelled in the alexa entity.

So I think I need to:

  1. Create a helper input_datetime
  2. Copy the state of the alexa entity to this helper
  3. Use this helper as the condition time for a second automation.

Does this sound right? Any example code for doing these steps?

Thanks!

The State of the next_alarm sensor appears to be a timestamp:

Trying to copy it to a input_datetime isn’t working:

When you created the Input Datetime helper, did you configure it to store both time and date?

If you did, post the automation that contains the input_datetime.set_datetime action.

If there is no automation yet, did you try to test the action using Developer Tools → Actions? If you did, it doesn’t support templates (thereby causing the action to fail).

Ok, I did have the helper set for time only, so I switched it to date and time. Still it’s not getting updated by this automation.

I tried both of these timestamp templates, neither worked.

action: input_datetime.set_datetime
metadata: {}
data:
#  timestamp: "{{ states('sensor.emma_s_echo_dot_next_alarm') }}"
  timestamp: "{{ state_attr('sensor.emma_s_echo_dot_next_alarm', 'timestamp') }}"
target:
  entity_id: input_datetime.emmaalarmstoredtime

The example you posted isn’t an automation. It’s YAML code for a single action.

Post the automation that contains the action.

If you’re attempting to test that action using Developer Tools-> Actions, you can’t because that feature doesnt support templates.

alias: EmmaAlarmBooleanOn
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.emma_s_echo_dot_next_alarm
    from: unknown
conditions: []
actions:
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.emmaalarmon
  - action: input_datetime.set_datetime
    metadata: {}
    data:
      timestamp: "{{ states('sensor.emma_s_echo_dot_next_alarm') }}"
    target:
      entity_id: input_datetime.emmaalarmstoredtime
mode: single

How are you testing the automation?

Are you waiting for the sensor’s state to change from unknown to a datetime string? Or are you executing the automation’s Run command?

In either case, what is the sensor’s current value?

I’ve tried it both by setting an alarm, and by “run actions”.

Here just now I set an alarm for 4p.

Here is the current state of the sensor:

the input_datetime isn’t updated to 4pm. When I look at the automation trace timeline:

Ok solved it - the data needs to be type datetime not timestamp

action: input_datetime.set_datetime
metadata: {}
data:
  datetime: "{{ states('sensor.emma_s_echo_dot_next_alarm') }}"
target:
  entity_id: input_datetime.emmaalarmstoredtime

Thanks for your help!

The error message is correct. It expects a float (floating point number) but it didn’t get one because the template didn’t provide it.

Change it to this:

timestamp: "{{ state_attr('sensor.emma_s_echo_dot_next_alarm', 'timestamp') }}" 

Not really.

  • If you use the action’s timestamp option then you must supply it with a numeric timestamp value.

  • If you use the action’s datetime option then you must supply it with a datetime string value.

Your posted automation used the timestamp option but supplied it with a datetime string value (incorrect value). That’s why the error message complained it wasn’t getting what it required.