is it possible to read a text file and use the input as trigger.
The text file has one line that says: “Light1 on” and then the light goes on. And after like 10 Minutes the text file has a second line “Light1 off” and the light turns off. Something like that.
I have added the sensor to my config and its working. But how do I put that into an automation?
The sensor now says Light 1 on - I need a trigger which executes when the sensor changes. So when another automation turns the light off he doesn’t read the sensor again and turns the light on again. I want to control about 3-4 things with this file so I guess in the automation has include some sort of if then.
But I don’t even know where to start with that.
Yes, of course. I want to use my XMPP Server to communicate with HA. I send a message to a specific user and a text file with the message is created.
This way I can send a message like Heater on or Light 45 on to controll HA.
No, I was already happy to have this file to read from. And I don’t use MQTT. Just need some kind of if else action and another trigger when the file changes and it would be perfect.
This does work but since he runs both service templates Iam getting these errors in log:
ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.file. Invalid data for call_service at pos 1: Service does not match format <domain>.<name>
And I would still need another trigger when the file changes with the same input so it does trigger then but not on reboot.
I agree you’re probably better off just having an automation per “event”. But if you must do it in one, then given the four “events” in your current example:
- id: File
alias: File
trigger:
- platform: state
entity_id: sensor.file
action:
- service_template: >
{% if 'on' in trigger.to_state.state %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
data_template:
entity_id: >
{% set d,n = trigger.to_state.state.split()[0:2] %}
{{ d|lower }}.{{ d|lower }}_{{ n }}
Do you mind explaining this code to me, what does it do?
Does this prevent a trigger wenn HA restarts? Or does it prevent the error logs? Or Both? What does the block under entity_id mean? Do I have to change something when I add more “events”?
Reading is one thing, understanding another. English is not my native language and sometimes I simply do not understand what things do. Trial and Error is how iam using HA most of the time…
Then it sounds like maybe you’re better off keeping things simple, which in this case, would mean writing an automation for each event with a simple state trigger, e.g.:
trigger:
- platform: state
entity_id: sensor.file
to: 'Switch 1 on'
action:
- service: switch.turn_on
entity_id: switch.switch_1
But, to explain…
The service_template determines if you’re trying to turn something on or off, then calls the appropriate homeassistant service. Note that these services can turn any type of entity on or off, so the service_template should suffice if you’re always sending a command containing on or off (and as long as the string on is not anywhere else in the “event.”)
data_template allows the use of templates to specify the values for other parameters, such as entity_id. In this case it’s splitting the “event” string up (using the default delimiter of white space.) Then it’s taking the first and second pieces. The first specifies the entity’s domain, and the second is the number. Then it uses those to “build” the corresponding entity_id value.
Thanks for the explanation. And Iam sorry but I still don’t get what your code above does.^^ I have added it and do not see any difference. Maybe my comments have been misleading.
I just want the errors in logs to disappear and that it triggers again if the file changes with the same Input. Even with the simple state trigger I would need the last point.
After some more testing (and some sleep ) I think I now understand what your script should do. Sadly it does not do it.
With nonsense inside the file I get something like that: Unable to find service asdf/turn_on. But with a input that should work like “switch 1 on” he does nothing and nothing appears in log. The other thing is, not everything that I want to control has a number.
So if you don’t have enough of my stupidity, and I won’t blame you if thats the case, I would be happy for some help
If you want a suggestion that will handle all the cases properly, you’ll have to define what all those cases are. I can’t guess what you might do. But even so, it seems you’re asking for someone to do the work for you, instead of trying to learn and do the majority of the work yourself. Again, if building a complicated automation is more than you can take on at this point in your learning curve, then maybe you should build more simplistic automations as opposed to fewer more complicated ones.
I had to do 3 dummy scripts because otherwise Iam getting a “Script already running” error.
Outside of HA a script is running which will overwrite the content of the file once a day so I don’t have the problem with the same Input. I’ll have to see if once a day is enough.^^
As you can see I wasn’t all lazy and hoping for a solution from you.
The solution works, but I guess there are more clever ways to do it. But thats over my capabilities.