#!/bin/bash while read TOPIC JSON do database=$(echo $TOPIC | cut -d '/' -f 1) table=$(echo $TOPIC | cut -d '/' -f 2-) schema='' case $TOPIC in 'domotic/radiator') schema='room_id int, radiator_id integer, powered boolean' ;; 'domotic/temperature') schema='room_id int, temperature numeric' ;; 'domotic/humidity') schema='room_id int, humidity numeric' ;; 'domotic/teleinfo') schema='adco text, optarif text, isousc smallint, hchc bigint, hchp bigint, ptec text, iinst int, imax int, papp int, hhphc text, motdetat text' ;; 'domotic/vmc') schema='speed integer, forced boolean' ;; 'domotic/weather') schema='temperature_indoor numeric, temperature_outdoor numeric, dewpoint numeric, humidity_indoor integer, humidity_outdoor integer, wind_speed numeric, wind_dir numeric, wind_direction text, wind_chill numeric, rain_1h numeric, rain_24h numeric, rain_total numeric, pressure numeric, tendency text, forecast text' ;; 'network/connected_device') schema='station_mac macaddr, ip inet, mac macaddr, virtual_mac macaddr' ;; 'system/log') schema='level text, priority numeric, facility text, date timestamp, host text, message text, pid text, program text' ;; esac if [[ $schema != '' ]] then psql -q -c "INSERT INTO $table SELECT now() AS created, * FROM json_to_record('$JSON') as x($schema);" "$database" fi done < <(mosquitto_sub -v -t '#')