Publish sensor value to wordpress

Hi, I’m looking for a way to publish temperature of a sensor, that is connected to a Raspberry, on a web page, that is hosted on Wordpress. I’ve setup publishing to MQTT, but unfortunately no MQTT addons on wordpress don’t fetch the values.
What are the other options?

You could use a REST call to the XML RPC interface.

https://codex.wordpress.org/XML-RPC_Support

Did you manage to solve this? I have a similar question.

I did a workaround. I publish temperature over MQTT, client picks up that value and saves it to a local TXT file, then a PHP script in wordpress reads that vaule and displays it in webpage.

Sgaluf, I’d be very interested in some more details on how you did this, I have a very sillier requirement.

1 Like

First the automation in HA, which publishes living room and outside temperature every 15 minutes:


automations:
  - alias: Publish temp to mqtt
    trigger: 
    platform: time_pattern
    minutes: '/15'
    seconds: 00 
    action:
    service: script.publish_out_mqtt
    
script:
  publish_out_mqtt:
    sequence:
      - service: mqtt.publish
        data_template:
          payload: "{{ states.sensor.dark_sky_temperature.state}}"
          topic: out/temperature
          retain: true
          
      - service: mqtt.publish
        data_template:
          payload: "{{ states.sensor.dark_sky_humidity.state}}"
          topic: out/humidity
          retain: true

      - service: mqtt.publish
        data_template:
          payload: "{{ now().day }}.{{ now().month }}.{{ now().year }} - {{ now().hour }}:{{ now().minute }}:{{ now().second }}"
          topic: out/time
          retain: true
          
      - service: mqtt.publish
        data_template:
          payload: "{{ states.sensor.living_room_temperature.state}}"
          topic: livingroom/temperature
          retain: true
          
      - service: mqtt.publish
        data_template:
          payload: "{{ states.sensor.living_room_humidity.state}}"
          topic: livingroom/humidity
          retain: true

      - service: mqtt.publish
        data_template:
          payload: "{{ now().day }}.{{ now().month }}.{{ now().year }} - {{ now().hour }}:{{ now().minute }}:{{ now().second }}"
          topic: livingroom/time
          retain: true

On the remte computer, I have running the following python script, which picks up data on MQTT and saves it to a local file

#!/usr/bin/python
# -*- coding: utf-8 -*-

import paho.mqtt.client as paho  #import the client1
import time
import ast
import codecs
import io

import sys
import argparse

file = open("/var/www/html/degree.txt","r")
degree=file.read()
file.close()

broker="192.168.1.10"
port=1883
#print(degree)

############
def on_message(client, userdata, message):

    temp = str(message.payload.decode("utf-8"))
    if message.topic[-11:]=="temperature":
        temp=str(int(float(message.payload)))+degree
        file.write(temp)
        print(temp)	

    if message.topic[-4:]=="time":
        file.write(temp)
        print(temp)

########################################

print("creating new instance")
client = paho.Client("P1") #create new instance
client.on_message=on_message #attach function to callback
print("connecting to broker")
client.connect(broker,port) #connect to broker

file = open("/var/www/html/temperature.txt", "w")

client.loop_start() #start the loop
print("Subscribing to topic","kitchen/temperature")
client.subscribe("kitchen/temperature")
time.sleep(1) # wait
client.loop_stop() #stop the loop

file.write("  |  ")

client.loop_start() #start the loop
print("Subscribing to topic","out/temperature")
client.subscribe("out/temperature")
time.sleep(1) # wait
client.loop_stop() #stop the loop

file.close()

file = open("/var/www/html/time.txt", "w")

client.loop_start() #start the loop
print("Subscribing to topic","kitchen/time")
client.subscribe("kitchen/time")
time.sleep(1) # wait
client.loop_stop() #stop the loop

file.write("\n")

client.loop_start() #start the loop
print("Subscribing to topic","out/time")
client.subscribe("out/time")
time.sleep(1) # wait
client.loop_stop() #stop the loop

file.close()

and finally, in my Wordpress page I have the followng PHP script, which reads out the values in the file “temperature.txt” and shows it on webpage

	<div style="float: left; width: 300px;border: double; height: 130px; ">
		<script type="text/javascript"
			src="http://code.jquery.com/jquery-1.10.1.min.js">
		</script>
		<script>
			$(document).ready(
				function() {
					$('#temperatura').load("temperature.txt")
						setInterval(function() {                   
						$('#temperatura').load("temperature.txt")
						}, 100000);
					});
		</script>
		<div align="center" style="width: 100%;background-color: rgba(204, 51, 0, 0.8);height: 130px;">
			<p style="color:#FFFFFF;font-size:20px;"><br>NOTRI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZUNAJ</p>

			<p id="temperatura" style="color:#FFFFFF;font-size:64px; padding: 0 px;">abc</p>
			<br>
		</div>
	</div>

Many thanks! I’ll have a “bash” at that :slight_smile:

An update… I’m making some headway, The only issue is that Home Assistant does not like your automations scripts for some reason (I’ve substituted your sensors for mine of course). I just get a number of errors from the automations.yaml file.

My Broker (actually running on a separate Ubuntu server VM) is working perfectly and I cans end test messages from the developer tools => services screen without issue and see the messages appear on a ssh session on the broker.

I wonder if Home Assistant have changed some syntax in a recent version?

This still looks overly complicated to me. You can create a simple automation that uses the REST API to publish it to WordPress.

I’ve attemtoed to do this with REST, I can pull my temperature with REST and make a POST to word press with rest, but IM having trouble creating the rest command yml entry to post sensor state to word press. Any wya you can point me in the right direction with what this should look like?

EDIT:

I Believe this is the wrong forum please remove. I will repost.

I just tied this plugin https://wpgetapi.com and it works i can get the state or any other value that the HA api provides
Test output result here Publish states Home-assistant in Wordpress | Famillien Petersen

And how did you get that to work? Some explanation please as i got all up and running with WPgetAPI but after that i am lost on how to get it to work with code snippets.