Is there any way to set an input_tex value within a sensor in configuration.yaml rather than in an autmation or a script?
Do you mean a template sensor?
Yes exactly that would help - not to update just the sensor itself - but in addition to that, to also set the value of an input_text
You can use the new action feature for trigger based template sensors (just tried it, works):
template:
- trigger:
platform: state
entity_id: input_boolean.test
to: 'on'
action:
service: input_text.set_value
data:
value: This is a test
target:
entity_id: input_text.test
sensor:
- unique_id: '202309191434'
name: Test
state: whatever
Perfect, thank you!
Can it have two actions? Meaning, updating two input_texts? Also within the sensor section I have some calculations, and the output of those calculations would have to be used within the actions section I wonder if local variables within one can be used in the other section or if the action section can be after the sensor section - I guess Iāll have to experiment and come back to you
Try it out
It took a little work but this syntax works. I have three input_text helpers, one of which is populated by a rest api call outside of home assistant. That is represented in the dashboard by a sensor we were discussing (so that is it not editable in the dashboard). The other two input_text helpers are also populated each time the first one changes, so that the two most recent values are kept and I have another sensor that keeps track of the duration between the last two updates (which is shown as another sensor with the last interval duration in a dashboard). It works pretty well now (and alllowed me to replace an automation with the āactionā section):
Two odd things I discovered were:
-
When I originally had the action section BELOW the sensor section, that the input_text.weewx_last_array_data" would hold the previous value (not the new value), so I had to use ātrigger.to_state.stateā there instead. In the sensor section however, āinput_text.weewx_last_array_dataā shows the newer value. (It might also have been an issue since I had the action section below the sensor section.)
-
When I remove the trigger section and have it just as a plain sensor, the action section (if still at the bottom) does not result in an error message when the code is checked before restarting home assistant. Also, at runtime that action code is then simply ignored (unless the sensor is a trigger sensor).
(I could probably simplify it even further by comparing the current time to ā.last_changedā data somewere instead of the other input_texts.)
template:
- trigger:
- platform: state
entity_id: input_text.weewx_last_array_data
action:
- service: input_text.set_value
data:
value: "{{ states('input_text.latest_weewx_receipt') }}"
target:
entity_id: input_text.previous_weewx_receipt
- service: input_text.set_value
data:
value: >
{% set ts = trigger.to_state.state %}
{% set ts = ts[:15:] + " " + "{}".format(now().year) %}
{% set time = strptime(ts, "%b %d %H:%M:%S %Y") %}
{% set str = time.strftime('%a %-m/%-d %-I:%M:%S%p') %}
{% set indx = str.find("M") %}
{% set oldstr = str[indx-1:indx+1] %}
{{ str.replace(oldstr,oldstr.lower()) }}
target:
entity_id: input_text.latest_weewx_receipt
sensor:
- name: "Weewx last array data formatted"
state: "{{ states('input_text.latest_weewx_receipt') }}"
unique_id: weewx_last_array_data_formatted
Thanks @pedolsky for your help.
Can you explain why you are using input text helpers?
As the docs state the trigger must fire for the actions to be executed. No trigger, no action.
Last changed doesnāt survive restart, so that would be unreliable.
I was using the input_text helpers so it can be formatted in nice readable way and saved (not lost on restart/reboot):
Trigger-based template sensors survive restart. Unless you need the user input ability of the input text helpers, your current method is just adding extra stuff to the state machine.
Can any sensor be updated by the home assistant rest API if they are not input sensors (as that is how all of the majority of the ones in the picture are updated - they are sensors derived from the input sensors that are updated by the REST API)?
If I understand the question correctly⦠Yes. The REST API allows posting states directly to entities, but this value is ephemeral if the entity has another āsource of truthā that can update it. Additionally, the HA REST API supports posting events with event data. Trigger-based Template sensor can be triggered by an event as well as use values from the event object in its state template.
That is a confusing sentence⦠How is the data being brought into HA?
I am making a rest api call (using curl) to update an input_text sensor. Then I have a state sensor in configuration.yaml is just for display purposes in the dashboard as non-editable and itās state is the text within the input sensor (with some nice formatting). I hide the input sensor and display the other sensor.
For example here is part of the code from the python script that currently populates the input_text sensor:
curl_command = [
"curl",
"--insecure", # This is all internal to the same network & VLAN
"-X",
"POST",
"-H",
f"Authorization: Bearer " + api_password,
"-H",
"Content-Type: application/json",
"-d",
payload_json,
api_url # URL of the input_text sensor
]
result = subprocess.run(curl_command, check=True, capture_output=True, text=True)
- and here is a related sensor that is based upon one of the input_text sensors that are updated from the above:
template:
# (Learned a trigger was not at all needed in this case)
# - trigger:
# - platform: state
# entity_id: input_text.weewx_last_sent_to_weathercloud
# sensor:
- sensor:
- name: "Weewx last sent to WeatherCloud"
state: >
{% set data = states('input_text.weewx_last_sent_to_weathercloud') | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
{% set ts = data.year ~ ' ' ~ (states('input_text.weewx_last_sent_to_weathercloud') | regex_findall('(.+)kruse-pi weewx') | first).strip() %}
{% set time = strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local %}
{% set str = time.strftime('%a %-m/%-d %-I:%M:%S%p') ~ ' (10 min)' %}
{% set indx = str.find("M (") %}
{% set oldstr = str[indx-1:indx+1] %}
{{ str.replace(oldstr,oldstr.lower()) }}
unique_id: weewx_last_sent_to_weathercloud
If there was a way to send a curl command to directly to update a sensor (such as sensor.weewx_last_sent_to_weathercloud shown above) which just shows on the dashboard as an uneditable amount of text that would be great (but Iād have to move the formatting logic somewhere else I guess) - I may try that out some time (but I wouldnāt save much other than getting rid of a hidden input_text sensor - and would lose the data between reboots - and my price for doing it would be to figure out how to put all of that formatting logic instead into my python script).
@KruseLuds
I ended up here from a Google search and after reading what you were trying to do I thought Iād point you to the RESTful integration. Honestly, it was made for this use case, and is more straightforward than any of the template sensor, input text helper, or even Python script ways you were trying to use.
Basically, you can define your api call, your polling interval, and pick what values from the result you want and it creates the sensor for you.
Itās also possible to do it using the command-line integration, (command line call rather than REST call) but since youāre using curl anyway, you may as well save some parsing work and go directly to the REST integration.
Not sure if this will still be useful to you, but since Google still directs people here I thought Iād chime in with the tools Iād use to do what you were doing.