but how & where would I do this? Do I need to create a new file (called mqtt?), and put all of my calculations in that file, and then reference the new mqtt file in my config file?
I have created a new file called mqtt, and put this line in my config file
mqtt: !include mqtt.yaml
Within the mqtt file I have a load of sections similar to this
### OIL TANK SENSOR ############################################################
### Time Stamp ######
sensor
- name: "Oil Last Time Stamp"
state_topic: "home/rtl_433"
value_template: >
{% if value_json is defined and value_json.id == 136628653 %}
{{ (value_json.time) }}
{% else %}
{{ states('sensor.oil_last_time_stamp') }}
{% endif %}
unit_of_measurement: 'UTC'
- name: "Oil Tank Quantity"
#etc etc etc
This is slightly different from what @123 Taras wrote above, but my thinking is that the line mqtt is omitted because the code is within a file called mqtt instead. But it does seem to be working!
EDITED TO ADD: I spoke too soon - I am unable to restart HA due to it not liking this line within my config.yaml file
That’s the rule for all cases where you redirect a domain key to use a separate file. For example, open the automations.yaml file and you’ll see the first line does not contain automation:
Failed to restart Home Assistant
The system cannot restart because the configuration is not valid:
Error loading /config/configuration.yaml: in "/config/configuration.yaml", line 18, column 7:
Unable to read file /config/mqtt.yaml.
This occurs whether or not I include the first line of mqtt
within my mqtt.yaml file - which I tried in case it made a difference.
The mqtt code continues to run, as my Dashboard is updating as I would expect.
So I take it that the above error is for some reason not able to read my config/mqtt.yaml file, which now starts
### OIL TANK SENSOR ############################################################
### Time Stamp ######
#mqtt
sensor:
- name: "Oil Last Time Stamp"
state_topic: "home/rtl_433"
value_template: >
{% if value_json is defined and value_json.id == 136628653 %}
{{ (value_json.time) }}
{% else %}
{{ states('sensor.oil_last_time_stamp') }}
{% endif %}
unit_of_measurement: 'UTC'
### Air At Top ######
#etc etc etc
Presumably I have still made an error with the syntax, but can’t see where.
I don’t see a syntax error. I used your example in my system and it was able to find mqtt.yaml and create the sensor without any complaints.
Here’s the relevant entry in configuration.yaml telling Home Assistant where manually configured MQTT entities are located.
Here are the contents of mqtt.yaml containing your sensor configuration:
After a restart (restarted because my configuration.yaml file did not originally have an mqtt: key) here is the sensor as it appears in Developer Tools > States. Its value is unknown because I have not published a value to its state_topic.
Apologies for the delay in replying but I have been away. It would appear that I had called my mqtt file mqtt and not mqtt.yaml, but I have now changed it and everything works. So thanks again, and sorry for being so stupid!