I’m using an input_datetime helper to allow users to see the conditions expected at the time they are interested in. A number of things are read from a REST call to an API which is somewhat rationed and I want to minimise the number of calls made to it. My current solution uses an automation triggered by the state of the input_datetime. I’ve just realised that it is getting triggered twice, when the user changes the date and when the time is changed. I’m using a picture-elements card to display the helper and when it is clicked on the normal HA interface comes up that allows the date & time to be changed. Naturally there is a time lag between these 2 actions and the lag depends on how quickly the user moves from one part of the display to the other, and I’ve seen that can take anything from milliseconds to tens of seconds. I’ve implemented a delay so the automation won’t trigger until the state change is n milliseconds old, but if I make this long enough to prevent the double call to the API, the interface feels unresponsive. I’ve thought about waiting for both the date and the time to change, but users often only want to change one of them.
Is there a different method for getting the user to set a date_time that only results in a single state change?
It is pure guess work without your YAML codes.
Thanks for taking a look. I had imagined my question was about how a user can set a date_time rather than these specifics, but here you go…
In a picture-elements card:
- type: state-label
style:
left: 50%
font-size: 110%
top: 0
transform: translate(-50%,-65%)
entity: input_datetime.tidequery
suffix: " (Click to change)"
The automation:
alias: tidequeryrun
description: Gets tidal data when query datetime changes
triggers:
- entity_id:
- input_datetime.tidequery
for:
hours: 0
minutes: 0
seconds: 3
trigger: state
conditions: []
actions:
- target:
entity_id:
- sensor.tidequeryheight
- sensor.tidequeryeventlast
- sensor.tidequeryeventlasth
- sensor.tidequeryeventnext
- sensor.tidequeryeventnexth
- sensor.sunrise_sunset_query
data: {}
action: homeassistant.update_entity
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 10
- action: homeassistant.update_entity
data:
entity_id:
- sensor.tidequeryrate
- sensor.tidequery
mode: single
Part of the REST API call:
- resource_template: https://admiraltyapi.azure-api.net/uktidalapi-premium/api/V2/Stations/0536/TidalHeights?StartDateTime={{ state_attr('sensor.tidequerytiming','StartDateTime') }}&EndDateTime={{ state_attr('sensor.tidequerytiming','EndDateTime') }}&IntervalInMinutes=2
scan_interval: 120000
headers:
Ocp-Apim-Subscription-Key: key
Cache-Control: no-cache
sensor:
- name: Tidequeryheight
unique_id: tidequeryheight
value_template: >
{% set idx = value_json.index(value_json|selectattr('DateTime', '==', state_attr('sensor.tidequery','query'))|first|default) -%}
{{ (value_json[idx]).Height|float(0) }}
state_class: measurement
unit_of_measurement: m
- name: Tidequerylaterheight
unique_id: tidequerylaterheight
value_template: >
{% set idx = value_json.index(value_json|selectattr('DateTime', '==', state_attr('sensor.tidequery','later'))|first|default) -%}
{{ (value_json[idx]).Height|float(0) }}
state_class: measurement
unit_of_measurement: m
I’m feeling foolish. All I need to do is add a separate trigger button to be pressed once the date_time is set. “Press to query”. Any better ideas?
That was my first thought, but I have not looked closer at the YAML, because I am not near my HA to do tests.
why aren’t you using a time trigger?
That trigger will fire whenever input_datetime.tidequery
changes state, not at the provided time.
- trigger: time
at: input_datetime.tidequery
I think they want it to do that on demand based on updating the UI, P. But I’m with you I’d have it check on a schedule and then a demand update on the dash to force a new time into the system when I want to change it.
This triggers on both state changes and attribute changed. That is probably why it fires multipe times, one for the state and one for an attribute. Try using an empty from: or consider:
- not_from:
- unvailable
- unknown
- not_to:
- unvailable
- unknown
Thank you all for your thoughts. To clarify: the user sets the input_datetime and the system immediately finds the tidal conditions that pertain to that time. I have been using the change of state to trigger the actions and my problem was the UI sees 2 changes when the user is selecting the datetime of interest. As indicated earlier, I am implementing a change so the user triggers the enquiry with a separate button after they have set the date time. If anyone is interested, I’ll post my code when I’ve debugged it.